/** 
 * Description:商品类别维护
 * Date:2011-11-14
 * Author:Along	
*/
package com.community.shopping.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.community.shopping.domain.GoodsType;
import com.community.shopping.service.logic.GoodsManager;
import com.community.shopping.service.logic.GoodsTypeManager;
import com.community.shopping.util.RequestHelp;

@Controller 
public class GoodTypeControl {
	@Autowired  
    private GoodsTypeManager goodsTypeManager;
	@Autowired  
    private GoodsManager goodsManager;
	
	/**
	 * 类别列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/typelist")
	 public ModelAndView getlist(HttpServletRequest request) 
   { 
     ModelAndView modelAndView = new ModelAndView("goods/goodstype_list.jsp"); 
     Integer shopId=RequestHelp.getShopId(request);
     List goodsTypes=goodsTypeManager.getTopGoodsType(shopId);
	 modelAndView.addObject("goodstypes", goodsTypes); 	 
     return modelAndView; 		
   } 
	
	/**
	 * 类别新增
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/typeadd")
	 public ModelAndView addtype(HttpServletRequest request) 
    { 
      ModelAndView modelAndView = new ModelAndView("goods/goodstype_add.jsp"); 
      Integer shopId=RequestHelp.getShopId(request);
      List goodsTypes=goodsTypeManager.getTopGoodsType(shopId);
	  modelAndView.addObject("goodstypes", goodsTypes); 
      return modelAndView; 		
    } 
	/**
	 * 类别修改
	 * @param request
	 * @return
	 */
	@RequestMapping("/admin/typeedit")
	 public ModelAndView edittype(HttpServletRequest request) 
   { 
      ModelAndView modelAndView = new ModelAndView("goods/goodstype_edit.jsp"); 
	  String id=request.getParameter("id");
	  GoodsType goodType=goodsTypeManager.getGoodsType(Integer.valueOf(id));
	  modelAndView.addObject("goodtype", goodType); 
     return modelAndView; 		

	  } 
	/**
	 * 保存类别
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/typesave")
	 public ModelAndView save(HttpServletRequest request,GoodsType goodsType) throws Exception 
  { 
      ModelAndView modelAndView = new ModelAndView("typelist.do");    
      String typeid=request.getParameter("typeid");
      String upid=request.getParameter("upid");
	  if(StringUtils.isBlank(typeid)) //新建
	  {	
		   if(Integer.valueOf(upid)>0)//有上级目录的子目录
		   {
		   GoodsType upGoodType=goodsTypeManager.getGoodsType(Integer.valueOf(upid)); 
		   goodsType.setUpGoodType(upGoodType);	
		   goodsType.setLevel(upGoodType.getLevel()+1);
		   }
		   else//顶级目录
		   {
			   goodsType.setLevel(1);  
		   }
		   Integer shopId=RequestHelp.getShopId(request);
		   goodsType.setShopId(shopId);
		  
	  }else//修改 
	  {
		  GoodsType oldGoodsType=goodsTypeManager.getGoodsType(Integer.valueOf(typeid)); 
		  goodsType.setId(oldGoodsType.getId());
		  goodsType.setUpGoodType(oldGoodsType.getUpGoodType());
		  goodsType.setLevel(oldGoodsType.getLevel());		 
	  }
		  
	  goodsTypeManager.save(goodsType);
    return modelAndView; 		

	  } 
	/**
	 * ajax方式删除类别
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/admin/typeremove")
	 public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception  
  { 
     int id=Integer.parseInt(request.getParameter("id"));
     GoodsType goodsType=goodsTypeManager.getGoodsType(id);
     response.setCharacterEncoding("UTF-8");
	  if(goodsType.getLevel()==3)
	  {
       List goods=goodsManager.getGoodsByType(goodsType);
       if(goods.size()>0)
       {
    	   response.getWriter().print(2); 
    	   return;
       }
	  }else if(goodsType.getUnderGoodTypes().size()>0)
	  {
		  response.getWriter().print(3); 
		  return;
	  }
	   goodsTypeManager.delete(goodsType);
	   response.getWriter().print(1); 

	  } 
	/**
	 * ajax方式获取子类别
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/admin/sublist")
	public void getSubList(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String goodsTypeId=request.getParameter("id");
		GoodsType goodType=goodsTypeManager.getGoodsType(Integer.valueOf(goodsTypeId));
		List<GoodsType> types = goodsTypeManager.getGoodsTypeByUp(goodType);
		StringBuilder sb=new StringBuilder("[");
		for(GoodsType goodstype:types)
		{
			if(sb.length()>1)
			{
				sb.append(",");	
			}
			sb.append("{");
			sb.append("\"id\":\""+goodstype.getId()+"\",");
			sb.append("\"typeName\":\""+goodstype.getTypeName()+"\",");
			sb.append("\"sort\":\""+goodstype.getSort()+"\",");
			sb.append("\"level\":\""+goodstype.getLevel()+"\"");
			sb.append("}");					
		}
		sb.append("]");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 	
		
	}
	/**
	 * ajax方式获取三级子类别
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/admin/thirdlist")
	public void getThirdList(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String goodsTypeId=request.getParameter("id");
		String subtypeids=request.getParameter("subtypeids");
		List<GoodsType> types = goodsTypeManager.getGoodsType("upGoodType.upGoodType.id="+goodsTypeId+" and status=1 order by sort,id");
		StringBuilder sb=new StringBuilder("[");
		for(GoodsType goodstype:types)
		{
			if(sb.length()>1)
			{
				sb.append(",");	
			}
			Integer id=goodstype.getId();
			sb.append("{");
			sb.append("\"id\":\""+id+"\",");
			sb.append("\"typeName\":\""+goodstype.getTypeName()+"\",");
			if(subtypeids.startsWith(id+",") || subtypeids.endsWith(","+id) || subtypeids.indexOf(","+id+",")>0)
			{
			sb.append("\"status\":\"1\",");	
			}
			else
			{
			sb.append("\"status\":\"0\",");		
			}
			sb.append("\"sort\":\""+goodstype.getSort()+"\"");
			sb.append("}");					
		}
		sb.append("]");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 	
		
	}
	@RequestMapping("/goodstype/query")
	public void getGoodsType(HttpServletRequest request,HttpServletResponse response)throws Exception
	{
		String goodsTypeId=request.getParameter("id");
		GoodsType goodType=goodsTypeManager.getGoodsType(Integer.valueOf(goodsTypeId));
		List<GoodsType> types = goodsTypeManager.getGoodsTypeByUp(goodType);
		StringBuilder sb=new StringBuilder("[");
		for(GoodsType goodstype:types)
		{
			if(sb.length()>1)
			{
				sb.append(",");	
			}
			sb.append("{");
			sb.append("\"id\":\""+goodstype.getId()+"\",");
			sb.append("\"typeName\":\""+goodstype.getTypeName()+"\",");
			sb.append("\"level\":\""+goodstype.getLevel()+"\"");
			sb.append("}");	
			if(goodstype.getLevel()==2)
			{
				List<GoodsType> subtypes= goodsTypeManager.getGoodsTypeByUp(goodstype);	
				for(GoodsType subgoodstype:subtypes)
				{
					sb.append(",");	
					sb.append("{");
					sb.append("\"id\":\""+subgoodstype.getId()+"\",");
					sb.append("\"typeName\":\""+subgoodstype.getTypeName()+"\",");
					sb.append("\"level\":\""+subgoodstype.getLevel()+"\"");
					sb.append("}");	
					
				}
			}
				
		}
		sb.append("]");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(sb.toString()); 	
		
	}
}
